#coding:utf-8

import scrapy

from kblu.items import KbluItem
import time

class KbluSpider(scrapy.Spider):
	name = 'kblugo'
	allowed_domains = ['6070lu.com']
	start_urls = [
		"http://6070lu.com/list/1.html",
		"http://6070lu.com/list/2.html",
		"http://6070lu.com/list/3.html",
		"http://6070lu.com/list/4.html",
		"http://6070lu.com/list/7.html"
	]
	site = "http://6070lu.com"

	def parse(self, response):
		details = response.xpath('//ul[@class="mlist"]/li/a/@href').extract()
		for mv in details:
			yield scrapy.Request(self.site+mv, self.parse_detail)
		next_page = response.xpath('//div[@id="pages"]/a/@href').extract()
		print next_page[-2]
		if next_page[-2] !=  next_page[-1]:
			print self.site+next_page[-2]
			yield scrapy.Request(self.site+next_page[-2], self.parse)

	def parse_detail(self, response):
		#title, img, thunder, updatetime, type, qqdl, ed2k
		item = KbluItem()
		try:
			item['title'] = response.xpath('//div[@class="info"]/h1/text()').extract()[0]
			item['updatetime'] = response.xpath('//div[@class="info"]/ul/li/div/text()').extract()[0]
			item['type'] = response.xpath('//div[@class="info"]/ul/li[1]/a/text()').extract()[0]
			item['thunder'] = response.css('.thunder > a::attr("href")').extract()[0]
			item['qqdl'] = response.css('.qqdl > a::attr("href")').extract()[0]
			item['ed2k'] = response.css('.ed2k > a::attr("href")').extract()[0]
			item['img'] = response.css('.pic > img::attr("src")').extract()[0]
			item['flashget'] = response.css('.flashget > a::attr("href")').extract()[0]
			item['size'] = response.xpath("//div[@class='play-list']/a/text()").re(r'\d*\.?\d*MB')[0]

		except Exception, e:
			print e

		yield item